from game import zz_game
from zz import zz_log

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)

    def get_name(self):
        return "跃动小子"

    def game_alert(self):
        """
        广告弹窗
        :return:
        """
        self.alert_close_by_file("./game/img/跃动小子")

    def play(self):
        self.log_msg("点击开宝箱")
        self.img_click("./game/img/跃动小子/play_stop.png", 10, 2)
        self.play_alert()

    def play_auto(self):
        self.log_msg("点击自动宝箱")
        self.img_click("./game/img/跃动小子/auto.png", 5)
        # self.img_click_if_exists('点击自动宝箱', "./game/img/跃动小子/auto.png", "./game/img/跃动小子/play.png")
        self.log_msg("点击开始")
        self.img_click("./game/img/跃动小子/play.png")

    def play_alert(self):
        """
        武器弹窗
        :return:
        """
        if not self.img_exit('./game/img/跃动小子/play_alert.png', 2):
            return False
        self.d.sleep(2)
        self.log_msg("存在弹窗，进行关闭")
        if self.img_exit('./game/img/跃动小子/play_2.png', 2):
            self.log_msg(f"战力下降")
            self.img_click('./game/img/跃动小子/play_0.png')
        else:
            self.log_msg(f"战力上升")
            self.img_click('./game/img/跃动小子/play_1.png')
            self.img_click('./game/img/跃动小子/play_0.png')

        if self.img_exit('./game/img/跃动小子/play_confirm.png', 1):
            self.log_msg("存在确认弹窗，进行关闭")
            self.img_click('./game/img/跃动小子/play_confirm_1.png')

        return True

    def handle_main(self):
        self.game_alert()

        self.log_msg("等待进入游戏页面")
        if not self.img_exit("./game/img/跃动小子/go.png", 20):
            return False
        self.log_msg("点击开始游戏")
        self.img_click("./game/img/跃动小子/go.png")
        self.log_msg("等待进入游戏主界面")
        self.img_exit("./game/img/跃动小子/auto.png")
        self.log_msg("已进入游戏主界面")

        self.game_alert()

        self.play_alert()

        if self.not_prize_handle():
            return False

        while not self.check_done():
            for i in range(5):
                self.play()
                self.d.sleep(1)

        return True
